﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Controls;
using FCursor = System.Windows.Forms.Cursor;
using System.Windows.Input;
using System.Threading;
using RPAVIEW.util;
using SUNRPA.CONTROLS.IMPL.domain.constant;
using SUNRPA.CONTROLS.IMPL.domain.flowItemParam;
using System.Windows;
using RPAVIEW.views.controlLibrary;

namespace RPAVIEW.views.EditPage.flows.MouseMoveN {
    /// <summary>
    /// Input.xaml 的交互逻辑
    /// </summary>
    public partial class Input:BaseParamViewAction{
        private Task? posTask;
        private CancellationTokenSource cancelTokenSource = new CancellationTokenSource();
        public Input() {
            InitializeComponent();
            posTask = Task.Factory.StartNew(() => {
                while (!cancelTokenSource.IsCancellationRequested) { 
                    int x = FCursor.Position.X;
                    int y = FCursor.Position.Y;
                    this.currentPos.Dispatcher.BeginInvoke(new Action(() => { 
                        this.currentPos.Content = x + "," + y;
                    }));
                    Thread.Sleep(100);
                }
            }, cancelTokenSource.Token);
        }

        public override void UpdateSource() {
            this.posX.GetBindingExpression(SelVarTextBox.text).UpdateSource();
            this.posY.GetBindingExpression(SelVarTextBox.text).UpdateSource();
        }

        private void TaskClose(object sender, System.Windows.RoutedEventArgs e) {
            cancelTokenSource.Cancel();
        }

        private void EnterPos() { 
            MouseMoveNParam param = this.DataContext as MouseMoveNParam;
            param.PosX = FCursor.Position.X.ToString();
            param.PosY = FCursor.Position.Y.ToString();
        }

        private void RegistHotKey(object sender, RoutedEventArgs e) {
            WindowUtil.Regist(Window.GetWindow(this), HotkeyModifiers.MOD_CONTROL, KeyInterop.VirtualKeyFromKey(Key.E), new WindowUtil.HotKeyCallBackHanlder(EnterPos));
        }
    }
}
